﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS_BEGINNER_TO_PROFICIENT.STD.ReflectionSTD
{
    public class MyClass
    {
        private int myField;
        public string MyProperty { get; set; }

        public void SayHello() => Console.WriteLine("Hello from Mycalss!");
    }

    public class ReflectionSTDP1
    {
        public static void Execute1()
        {
            Type type = typeof(MyClass);

            Console.WriteLine("Class Name: " + type.Name);
            Console.WriteLine("Class NameSpance: " + type.Namespace);

            object instance = Activator.CreateInstance(type);
            Console.WriteLine("Instance create: " + instance.GetType().Name);

            var method = type.GetMethod("SayHello");
            method.Invoke(instance, null);

            var field = type.GetField("myField", System.Reflection.BindingFlags.NonPublic |
                System.Reflection.BindingFlags.Instance);
            field.SetValue(instance, 42);
            Console.WriteLine("Filed Value: " + field.GetValue(instance));

            var property = type.GetProperty("MyProperty");
            property.SetValue(instance, "New Value");
            Console.WriteLine("Property Value: " + property.GetValue(instance));
        }
    }
}
